var Server = Ext.extend(Object, {

	initComponent : function() {
		this.url = this.getRootPath();
	},
	getRootPath : function() {
		var strFullPath = window.document.location.href;
		var strPath = window.document.location.pathname;
		var pos = strFullPath.indexOf(strPath);
		var prePath = strFullPath.substring(0, pos);
		var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
		return (prePath + postPath + '/');
	},
	getUrl : function(para) {
		
		if(!para){
			return this.getRootPath();
		}
		
		var act = this.getAct(para);
		
		var url = this.getRootPath() + para.resource;	
		if(act != ''){
			url = url + '?act=' + act;
		}
		return url;
	},
	getAct : function(para) {
				
		var act = '';
		
		if(para.act == 'r'){
			act = 'read';
		}else if(para.act == 'c'){
			act = 'create';
		}else if(para.act == 'u'){
			act = 'update';
		}else if(para.act == 'd'){
			act = 'delete';
		}
		
		return act ;
	},
	request : function(para, isSynchronous) {
		
		if (isSynchronous) {
			return this.synchronousRequest(para);
		} else {
			this.ajaxRequest(para);
		}

	},

	ajaxRequest : function(para) {
		
		var ajaxConfig = this.createAjaxConfig(para);
		this.configParams(ajaxConfig, para);
		this.configSuccessFunction(ajaxConfig, para);
		Ext.Ajax.request(ajaxConfig);
		
	},
	createAjaxConfig : function(para) {

		var url = this.getUrl(para);
		var ajaxConfig = {
			url : url,
			method : 'post'
		};

		return ajaxConfig;

	},
	configParams : function(ajaxConfig, config) {

		
		ajaxConfig.params = config.params;

	},
	configSuccessFunction : function(ajaxConfig, para) {

		ajaxConfig.success = function(response, options) {
			
			var responseArray = Ext.util.JSON.decode(response.responseText);
			
			if (!para.scope) {
				para.scope = this;
			}
			para.cbf.call(para.scope, responseArray);

		}

		return ajaxConfig;

	},
	configRequestPara : function(params, requestPara) {
		if (requestPara) {
			for (var p in requestPara) {
				params[p] = requestPara[p];
			}
		}
	},
	// 同步调用。
	synchronousRequest : function(para) {

		var url = this.getUrl(para);
		
		var requestPara = null;

		if (para.params) {
			requestPara = '';
			for (var p in para.params) {
				requestPara = requestPara + p + '=' + para.params[p]
						+ '&';
			}
		}
		
		var ela = Ext.lib.Ajax;
		var conObj = ela.getConnectionObject();
		var xr = conObj.conn;
		xr.open("POST", url, false);
		var headers = {};
		headers['Content-Type'] = "application/x-www-form-urlencoded; charset=UTF-8";

		for (var F in headers) {
			xr.setRequestHeader(F, headers[F])
		}
		xr.send(requestPara);
		// 成功状态码为200
		if (xr.status == "200") {
			var resObj = Ext.util.JSON.decode(xr.responseText);
			return resObj;
		} else {
			return null;
		}

	}

});